package com.five.wisdomEat.controller;

import com.five.wisdomEat.entity.Result;
import com.five.wisdomEat.until.AliOssUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class FileUploadController {

    @Value("${images.path}")
    private String basePath;

    /**
     *文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        //把文件内容存储到本地磁盘
        String originalFilename = file.getOriginalFilename();
        //保证文件名字唯一
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        //创建一个目录
        //File dir = new File(basePath);
        //判断目录是否存在
        //if(!dir.exists()){
            //如果目录不存在就直接创建
          //  dir.mkdirs();
    //    }
       // file.transferTo(new File(basePath+filename));
       // Result<String> result = new Result<>();
       // result.setData(filename);
        String url = AliOssUtil.uploadFile(filename,file.getInputStream());
        return Result.success(url);
    }
}
